package com.salesys.gr.gui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import com.salesys.gr.util.FrmHelper;
import com.salesys.gr.util.MsgBox;
import com.salesys.gr.util.LoginedUserHelper;
import com.salesys.tony.domain.User;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import com.salesys.tony.service.UserService;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.TraverseEvent;

public class FrmModifyPwd {

	protected Shell FrmModifyPwd;
	private Text txtPassword;
	private Text txtNewPwd_1;
	private Text txtNewPwd_2;

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		FrmModifyPwd.open();
		FrmModifyPwd.layout();
		while (!FrmModifyPwd.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		FrmModifyPwd = new Shell(SWT.CLOSE | SWT.APPLICATION_MODAL);
		FrmModifyPwd.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent e) {
				if (e.keyCode == 27) {
					FrmModifyPwd.close();
				}
			}
		});
		FrmModifyPwd.setSize(270, 199);
		FrmModifyPwd.setText("修改密码");
		// 窗口居中
		FrmModifyPwd.setLocation(new FrmHelper().setLocationCenter(FrmModifyPwd));

		Label lblOldPwd = new Label(FrmModifyPwd, SWT.NONE);
		lblOldPwd.setBounds(49, 34, 61, 17);
		lblOldPwd.setText("原密码：");

		Label lblNewPwd_1 = new Label(FrmModifyPwd, SWT.NONE);
		lblNewPwd_1.setBounds(49, 60, 61, 17);
		lblNewPwd_1.setText("新密码：");

		Label lblNewPwd_2 = new Label(FrmModifyPwd, SWT.NONE);
		lblNewPwd_2.setBounds(49, 86, 61, 17);
		lblNewPwd_2.setText("新密码：");

		txtPassword = new Text(FrmModifyPwd, SWT.BORDER | SWT.PASSWORD);
		txtPassword.setBounds(116, 31, 100, 20);

		txtNewPwd_1 = new Text(FrmModifyPwd, SWT.BORDER | SWT.PASSWORD);
		txtNewPwd_1.setBounds(116, 57, 100, 20);

		txtNewPwd_2 = new Text(FrmModifyPwd, SWT.BORDER | SWT.PASSWORD);
		txtNewPwd_2.setBounds(116, 83, 100, 20);

		// 修改密码
		Button btnModify = new Button(FrmModifyPwd, SWT.NONE);
		btnModify.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 13) {
					modifyPwd();
				}
			}
		});
		btnModify.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				modifyPwd();
			}
		});
		btnModify.setBounds(166, 131, 88, 30);
		btnModify.setText("修　改");
	}

	public void modifyPwd() {
		User modifyPwdUser = new User(LoginedUserHelper.getLoginedUser());
		int affectOld = 0;
		try {
			affectOld = new UserService().verifyOldPwd(txtPassword.getText(), modifyPwdUser);
		} catch (Exception e) {
			e.printStackTrace();
		}
		int affectNew = 0;
		try {
			affectNew = new UserService().verifyNewPwd(txtNewPwd_1.getText(), txtNewPwd_2.getText());
		} catch (Exception e) {
			e.printStackTrace();
		}
		modifyPwdUser.setPassword(txtNewPwd_1.getText());
		if (txtPassword.getText() == "") {
			new MsgBox().errorTip(FrmModifyPwd, "原密码不能为空");
		} else if (txtNewPwd_1.getText() == "") {
			new MsgBox().errorTip(FrmModifyPwd, "新密码不能为空");
		} else if (txtNewPwd_2.getText() == "") {
			new MsgBox().errorTip(FrmModifyPwd, "新密码不能为空");
		} else if (txtNewPwd_1.getText().length() < 6) {
			new MsgBox().errorTip(FrmModifyPwd, "密码不能小于六位");
		} else if (affectNew == 0) {
			new MsgBox().errorTip(FrmModifyPwd, "两次输入新密码不一致");
		} else if (affectOld == 0) {
			new MsgBox().errorTip(FrmModifyPwd, "原密码不正确！");
		} else {
			int affectRow = 0;
			try {
				affectRow = new UserService().modifyPwd(modifyPwdUser);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (affectRow == 0) {
				new MsgBox().errorTip(FrmModifyPwd, "修改失败！");
			} else {
				new MsgBox().infoTip(FrmModifyPwd, "修改成功！");
				LoginedUserHelper.fresh();
				FrmModifyPwd.close();
			}
		}
	}
}